package com.shigen.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.shigen.demo.annotation.RequestLimit;
import com.shigen.demo.bean.PageRequest;
import com.shigen.demo.bean.Result;
import com.shigen.demo.entity.req.UserReq;
import com.shigen.demo.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "user")
@Api(tags = "密码表的相关操作")
public class UserController {

  @Autowired
  private IUserService userService;

  @PostMapping(value = "login")
  @RequestLimit(second = 3)
  @ApiOperation(value = "用户登录")
  public Result Login(@RequestBody @Valid UserReq.Login login, HttpServletRequest request) {
    return userService.Login(login, request);
  }

  @PostMapping("online")
  @ApiOperation(value = "获得在线用户信息的分页")
  public Result<JSONObject> getOnlineUser(@RequestBody @Valid PageRequest page) {
    return Result.ok(userService.getOnlineUser(page));
  }

  @GetMapping(value = "onlinecount")
  @ApiOperation(value = "获得在线用户数量")
  public Long getOnlineCount() {
    return userService.getOnlineUserCount();
  }

  @PostMapping("/logout/{id}")
  @ApiOperation(value = "注销登录")
  public Object Login(@RequestHeader("token") String token, @PathVariable("id") Integer id) {
    return userService.forceLogOut(token, id);
  }

  @DeleteMapping(value = "forcelogout/{id}")
  @ApiOperation(value = "强制注销登录")
  public Result forceLogout(@RequestHeader("token") String token, @PathVariable("id") Integer id) {
    return userService.forceLogOut(token, id);
  }
}
